﻿using T4UtilityBelt.UxDesigns;
using T4UtilityBelt.UxDesigns.Controls;

namespace T4UtilityBelt.DataDesigns
{
    public class ParentOne : Parent
    {
        public ParentOne() 
            : base(null, null)
        {
            Multiplicity = Multiplicity.One;
        }

        public ParentOne(string name, string toName)
            : base(name, toName)
        {
            Multiplicity = Multiplicity.One;
        }

        public ParentOne(string name, string toName, bool required)
            : base(name, toName, required)
        {
            Multiplicity = Multiplicity.One;
        }

        public override bool IsMultiplicitySpecified
        {
            get { return Multiplicity != Multiplicity.One; }
        }

        //protected override PropertyTypeInfo PropertyTypeInfo
        //{
        //    get
        //    {
        //        if (PropertyTypeInfoProvider.PropertyTypes.ContainsKey(EntityModel.KeyType))
        //        {
        //            return PropertyTypeInfoProvider.PropertyTypes[EntityModel.KeyType];
        //        }
        //        return null;
        //    }
        //}

        public override Control CreateControl()
        {
            return new ParentOneControl(this);
        }

        public override string RenderInterfaceServiceCs()
        {
            return string.Format(
@"{2} {0}Id {{ get; set; }}
I{1}Item {0} {{ get; set; }}
"
, Name, ToName, EntityModel.KeyType);
        }

        public override string RenderModelCs()
        {
            return string.Format(@"
        /// <summary>
        /// Gets or sets the {0}.
        /// </summary>
        /// <value>
        /// The {0}Id.
        /// </value>
        public {2} {0} 
		{{ 
			get 
			{{ 
				return _{0};
			}} 
			set 
			{{ 
                if ({0} != value)
                {{
                    this.On{0}Changing(value);
                    this._{0} = value;
                    this.On{0}Changed();
                    this.OnPropertyChanged(""{0}"");
				    if (value != null)
				    {{
					    _{0}Id = value.Id;
				    }}
                }}
			}} 
		}}
		private {2} _{0} = new {2}();
        partial void On{0}Changing({2} value);
        partial void On{0}Changed();

        public {1} {0}Id
        {{
            get
            {{
                return this._{0}Id;
            }}
            set
            {{
                if ({0}Id != value)
                {{
                    this.On{0}IdChanging(value);
                    this._{0}Id = value;
                    this.On{0}IdChanged();
                    this.OnPropertyChanged(""{0}Id"");
                }}
            }}
        }}

        private {1} _{0}Id;
        partial void On{0}IdChanging({1} value);
        partial void On{0}IdChanged();

", Name, PropertyType, ToName);
        }

        public override string RenderAssignFromModelToServiceCs(string toControl, string fromControl)
        {
            return string.Format(
@"			if ({1}.{0}Id != {2}.{0}Id)
			{{
				{2}.{0}Id = {1}.{0}Id;
            }}", Name, toControl, fromControl);
        }

        public override string RenderAssignFromServiceToModelCs(string toControl, string fromControl)
        {
            return string.Format(
@"
			if ({1}.{0}Id != {2}.{0}Id)
			{{
				{1}.{0}Id = {2}.{0}Id;
				{1}.{0} = Mapper.CreateModelFromService<{4}, I{4}Item>({2}.{0});
			}}", Name, toControl, fromControl, PropertyType, ToName);
        }
    }
}